package org.terrier.matching.models.scorefitting;
import java.io.FileWriter;
import java.io.IOException;

import org.terrier.applications.*;
import org.terrier.utility.*;

public class FitScore{
	public static void main(String[] args){
		
		int Isprepare = 0;
		int Version = 2;//set the method computing the mean and sigma
		int GraphCount = 0;
		double [][] ParameterStore;//store the mean and sigma
		double [][] ProductStore;//store the Tf * L
		int [][][] DocTfMem;
		String StrAddr = ApplicationSetup.getProperty("scorefitting.folder", "");//set the directory for the doc I/O
		String trecOpion[] = {"-r"};
		String modelStr = "LognormalModel";//"LognormalModel";TF_IDF
		
		 
		// System.setProperty("terrier.home", "D:\\IR-test\\terrier-3.0");
		ApplicationSetup.bootstrapInitialisation();//set the terrior.home
		
		
		InputAndOutput iao = new InputAndOutput();
		String[] QueryMemory = iao.ReadQueryTerms(StrAddr);//read the query word list
		GetParameter gp = new GetParameter();
		int[] DocLength = gp.GetDocLength();//compute the DocLength
		// ComputingParameter cp = new ComputingParameter(DocLength,Version);//initialize the ComputingParameter class
		ParameterStore = new double[QueryMemory.length][];//initialize the array storing the mean and sigma whose dimension is L * 2
		ProductStore = new double[QueryMemory.length][];
		DocTfMem = new int[QueryMemory.length][][];
		//System.out.println("the prepraretion have been succeed");
		if(Isprepare == 1){
		int N = gp.GetN();
		//iao.Outputtest(StrAddr);
		//iao.OutputGraph(StrAddr+N+"_DocLength.jpeg", DocLength, "DocLength");
		for(int i = 0; i < QueryMemory.length; i++){
			int[][] DocTf = gp.GetTf(QueryMemory[i]);
			
/*			ParameterStore[i] = cp.GetMeanAndSigma(DocTf);	
			ProductStore[i] = cp.getDotProduct(DocTf);*/
			
			if(DocTf != null){
				DocTfMem[i] = DocTf;
				//iao.OutputGraph(StrAddr+"Graph_Tf//"+QueryMemory[i]+"_"+GraphCount+"_Tf.jpeg", DocTf[1], QueryMemory[i]+"_Tf");
				//iao.OutputGraph(StrAddr+"Graph_Product//"+QueryMemory[i]+"_"+GraphCount+"_Product.jpeg", ProductStore[i], QueryMemory[i]+"_Tf * DocLength");
				GraphCount++;
			}
		}
		//System.out.println("writeDocDocTfMem00 "+DocTfMem.length+" "+DocTfMem[1].length+" "+DocTfMem[2].length);
		iao.OutputDocLengthAndTf(StrAddr,N,DocTfMem,DocLength);
		try{
			int k = 0;
			int bound = 80;
			FileWriter fw = new FileWriter(StrAddr + N+"_" + "DocTfID.txt");
			FileWriter fw1 = new FileWriter(StrAddr + N+"_" + "DocTf.txt");
			//System.out.println("writeDocDocTfMem0 "+DocTfMem[0].length+" "+DocTfMem[1].length+" "+DocTfMem[2].length);
			for(int i = 0;i < QueryMemory.length;i++){
				int[][] DocTf = gp.GetTf(QueryMemory[i]);
				if(DocTf != null){
				for(int j = 0;j < DocTf[1].length;j++){
						fw.write(Integer.toString(DocTf[0][j]));
						fw.write(",");
						fw1.write(Integer.toString(DocTf[1][j]));
						fw1.write(",");
						k++;
						if(k > bound){
							fw.write(ApplicationSetup.EOL);
							fw1.write(ApplicationSetup.EOL);
							k=0;
						}
				}
				fw.write(Integer.toString(-1));
				fw.write(",");
				fw1.write(Integer.toString(-1));
				fw1.write(",");
				k++;
				if(k > bound){
					fw.write(ApplicationSetup.EOL);
					fw1.write(ApplicationSetup.EOL);
					k=0;
				}
				}
			}
			  }catch(Exception e){
				  e.printStackTrace();
			  }
		//iao.OutputMeanAndSigma(StrAddr, Version,N,ParameterStore);
		System.out.println("model preparing succesed");
		}
		try {
			/*
			TrecTerrier trecTerrier = new TrecTerrier();
			int status = trecTerrier.processOptions(trecOpion);
			trecTerrier.applyOptions(status);*/
			TRECQueryingHK trecQuerying = new TRECQueryingHK();
			trecQuerying.processQuerieshk(0d, false,"org.terrier.matching.models." + modelStr);
			trecQuerying.close();

		} catch (Exception e) {
			System.err.println("A problem occurred: "+ e);
			e.printStackTrace();
			System.exit(1);
		} catch (java.lang.OutOfMemoryError oome) {
			System.err.println(oome);
			oome.printStackTrace();
			System.exit(2);
		}
		System.out.println("retrevial have been fulfilled");
		String trecOpion1[] = {"-e"};
		try {
			TrecTerrier trecTerrier = new TrecTerrier();
			int status = trecTerrier.processOptions(trecOpion1);
			trecTerrier.applyOptions(status);
		} catch (Exception e) {
			System.err.println("A problem occurred: "+ e);
			e.printStackTrace();
			System.exit(1);
		} catch (java.lang.OutOfMemoryError oome) {
			System.err.println(oome);
			oome.printStackTrace();
			System.exit(2);
		}
		System.out.println("evaluation have been fulfilled");
		//return ParameterStore;*/
	}
}